﻿using Furion.DatabaseAccessor;
using Furion.DynamicApiController;
using Furion.FriendlyException;
using Furion.UnifyResult;
using JoyAdmin.Core;
using Mapster;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using TulingMember.Application.Dto;
using TulingMember.Core;

namespace TulingMember.Application
{
    /// <summary>
    /// 部门管理
    /// </summary>
    public class DepartmentService : IDynamicApiController
    {
        private readonly IRepository<cts_Department> _departmentRepository;

        public DepartmentService(IRepository<cts_Department> departmentRepository)
        {
            _departmentRepository = departmentRepository;
        }
        /// <summary>
        /// 列表
        /// </summary> 
        public List<cts_Department> SearchDepartment(BaseInput input)
        {
            var search = _departmentRepository.AsQueryable();
            if (!string.IsNullOrEmpty(input.keyword))
            {
                search = search.Where(m => m.Name.Contains(input.keyword));
            }
            return search.OrderBy(m => m.Sort).ToList();
        }

        /// <summary>
        /// 获取树形商品分类
        /// </summary>
        /// <returns></returns>
        public List<TreeDepartmentDto> GetTreeDepartment()
        {
            var list = _departmentRepository.AsQueryable().Select(u => u.Adapt<TreeDepartmentDto>()).ToList();
            return new TreeBuildUtil<TreeDepartmentDto>().Build(list);
        }
        /// <summary>
        /// 保存部门
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [SecurityDefine("department")]
        [UnitOfWork]
        public void SaveDepartment(cts_Department input)
        {
            var oldCount = _departmentRepository.Where(m => m.Name.Equals(input.Name) && m.Id != input.Id).Count();
            if (oldCount > 0)
            {
                throw Oops.Bah("名称已存在，请检查").StatusCode(ErrorStatus.ValidationFaild);
            }
            input.ParentIds = CreateNewPids(input.ParentId);
            if (input.Id > 0)
            {
                _departmentRepository.Update(input);
            }
            else
            {
                _departmentRepository.Insert(input);
            }
        }

        [NonAction]
        private string CreateNewPids(long pid)
        {
            if (pid == 0L)
            {
                return "[0],";
            }
            else
            {
                var pmenu = _departmentRepository.DetachedEntities.FirstOrDefault(u => u.Id == pid);
                return pmenu.ParentIds + "[" + pid + "],";
            }
        }

        /// <summary>
        /// 获取 
        /// </summary>  
        public cts_Department GetDepartment(long id)
        {
            return _departmentRepository.FindOrDefault(id);
        }

        /// <summary>
        /// 删除 
        /// </summary>  
        [SecurityDefine("department ")]
        public void DeleteDepartment(long id)
        {
            _departmentRepository.FakeDelete(id);
        }
    }
}
